function write_memory_to_file(filename, offsets_map)
    fid = fopen(filename, 'w');
    if fid == -1
        error('错误: 无法写入内存文件: %s', filename);
    end
    
    keys = cell2mat(offsets_map.keys);
    if isempty(keys)
        fclose(fid);
        return;
    end
    
    sorted_keys = sort(keys);
    
    for i = 1:length(sorted_keys)
        k = sorted_keys(i);
        v = offsets_map(k);
        if i > 1
            fprintf(fid, '\n');
        end
        fprintf(fid, '%d:%d', k, v);
    end
    fclose(fid);
end